function changeSpecialGroup() {
	let value = $("input:radio[name='special_group']:checked").val();
	console.log(value);
	if (value === 'true') {
		dealFieldUI('characteristic15', 1, 1);
		dealFieldUI('characteristic3', 0, 0);
		dealFieldUI('characteristic4', 0, 0);
	} else {
		dealFieldUI('characteristic15', 0, 0);
		dealFieldUI('characteristic3');
		dealFieldUI('characteristic4');
	}
}


let desc_data = {
	'select': 0,
	'mode': 'one'
};

let index = 2;
let currentChangeIndex = 1;
let count = 1;
let label;
let description;
let moreDescription;

function add(...obj) {
	let desc = '';
	for (let item of obj) {
		if (item != undefined) {
			desc += item;
		}
	}
	return desc;
}

function addField(field, value) {
	if (!value) {
		value = desc_data[field];
	}
	let separators = fields_s[field];
	if (undefined === separators) {
		console.log('error :' + field);
	}
	return add(separators[0], value, separators[1]);
}


function hasValue(field) {
	let value = desc_data[field];
	if (value) {
		return true;
	}
	return false;
}

function dealAllValue(select) {
	for (let field of fieldsThatProvideValue) {
		let charatoristics = definition[field];
		if (undefined === charatoristics) {
			console.log('error:' + field);
		}
		let def = charatoristics[select];
		if (undefined === def) {
			continue;
		}
		let exist = def[0];
		if (exist === 1) {
			if (field === 'characteristic3') {
				if (count > 1) {
					//处理多度数问题
					for (let i = 2; i <= count; i++) {
						let moreDesc = moreDescription[i - 2];
						let value = desc_data['moreDegree' + i];
						moreDesc += addField(field, value);
						moreDescription[i - 2] = moreDesc;
					}
				}
				if (hasValue(field)) {
					description += addField(field);
				}
			} else {
				if (hasValue(field)) {
					let value = addField(field);
					description += value;
					if (count > 1) {
						//处理多度数问题 //1处描述分发多处
						for (let i = 2; i <= count; i++) {
							let moreDesc = moreDescription[i - 2];
							moreDescription[i - 2] = moreDesc + value;
						}
					}
				}
			}
		}
	}
}

function genOneModeDesc(select) {
	description = '';
	moreDescription = [];
	for (let i = 2; i <= count; i++) {
		moreDescription[i - 2] = '';
	}

	dealAllValue(select);

	$("#shortDesc").val(description);
	$("#materialDesc").val(description);
	if (count > 1) {
		for (let i = 2; i <= count; i++) {
			let moreDesc = moreDescription[i - 2];
			$("#shortDesc" + i).val(moreDesc);
			$("#materialDesc" + i).val(moreDesc);
		}
	}
}

function desc(obj) {
	let id = obj.id;
	let value = getObjValue(obj);
	if (id != undefined) {
		desc_data[id] = value;
	}

	let select = desc_data['select'];
	let mode = desc_data['mode'];
	if ('one' === mode) {
		genOneModeDesc(select);
	}
	currentChangeIndex = 1;
}

function colorChange(obj) {
	desc_data['color'] = getObjValue(obj);
	let value = add(desc_data['characteristic2'], desc_data['color']);
	let ch2 = $("#characteristic2");
	ch2.val(value);
	desc(ch2[0]);
}

function changeOtherDegree(obj) {
	let value = getObjValue(obj);
	let index = $(obj).parent()[0].id.substr(14);
	desc_data['moreDegree' + index] = value;
	currentChangeIndex = index;

	desc(obj);
}

function addDegree() {
	let moreDegree = $("#moreDegree");
	moreDegree.append('<div id=moreDegreeItem' + index + '><label class="label-my-line">度数' + index +
		"</label><input name='characteristic4' input=text onchange='changeOtherDegree(this)' /><span class='required'>*</span>" +
		"<input type='button' class='decrase' value='-' onclick='removeDegree(this)'/><div/>");

	$("#moreDescrible").append(
		'<div class="row row-my-line"><div class="col-md-12"><label class="label-my-line">短描述' + index +
		'：</label><input name="shortDesc" id="shortDesc' + index + '"  htmlEscape="false" maxlength="40"' +
		'class="input-medium input-extent" />最多40个字</div></div>');
	$("#moreDescrible").append(
		'<div class="row row-my-line"><div class="col-md-12"><label class="label-my-line">长描述' + index +
		'：</label><input name="materialDesc" id="materialDesc' + index + '"  htmlEscape="false" maxlength="200"' +
		'class="input-medium input-extent" /></div></div>');
	$("#moreDescrible").append(
		'<div class="row row-my-line"><div class="col-md-12"><label class="label-my-line">物料超长描述' + index +
		'：</label><input name="longDesc" id="longDesc' + index + '"  htmlEscape="false" maxlength="200"' +
		'class="input-medium input-extent" /></div></div>');

	index++;
	count++;
}

function removeDegree(obj) {
	let button = $(obj)
	let text = button.prev().prev().prev().text();
	let i = text.substr(2);
	let parent = button.parent();
	parent.remove();
	$("#shortDesc" + i).parent().parent().remove();
	$("#materialDesc" + i).parent().parent().remove();
	$("#longDesc" + i).parent().parent().remove();
	count--;
}

function dealFieldUI(field, exist, required, value) {
	let charatoristics = definition[field];
	let def = charatoristics[desc_data['select']];
	if (undefined === def) {
		return;
	}

	if (exist === undefined) {
		exist = def[0];
	}
	if (required === undefined) {
		required = def[1] === 1;
	}
	console.log('field :' + field + " exist :" + exist + ' required:' + required + ' value:' + value);
	let source = $("#" + field);
	if (field === 'color') {
		let prev = source.prev();
		prev.text(label + '颜色');
	}
	let parent = source.parent().parent();
	let span = source.next();
	let isHidden = parent.hasClass('hidden');
	if (exist === 1 && isHidden) {
		parent.removeClass("hidden")
		parent.addClass("show");
		// let dynamicsFunctionName = 'fire_' + field;
		// invoke(dynamicsFunctionName);
		autoFillSelectValueByDefinitions(field, value);
		if (undefined != value) {
			source.val(value);
		}
		if (required) {
			span.attr("class", "required");
		} else {
			span.attr("class", "hidden");
		}
	} else if (exist === 0 && !isHidden) {
		parent.removeClass("show")
		parent.addClass("hidden");
	}
}

function queryDictAndGenSelect2(id, dictType, value) {
	// doGet(ctx_ + "/sys/dict/getDictListByType?type=" + dictType, (res => {
	let select = $("#" + id);
	let data = [{'value': 1, "label":"一"}, {"value":'0001', 'label':'二'}];
	for (let item of data) {
		if (!value || item.value != value) {
			select.append('<option value="' + item.value + '">' + item.label + '</option>');
		} else {
			select.append('<option value="' + item.value + '" selected="selected">' + item.label + '</option>');
		}
	}
}

function getDictTypeByDefinitions(id) {
	let otherInfo = otherUiInfo[id];
	if (!otherInfo) {
		return undefined;
	}
	let type = otherInfo['dictType'];
	if (Array.isArray(type)) {
		return type[desc_data['select']];
	} else {
		return type;
	}
}


function autoFillSelectValueByDefinitions(id, value) {
	let dictType = getDictTypeByDefinitions(id);
	if (undefined != dictType) {
		setTimeout(() => {
			queryDictAndGenSelect2(id, dictType, value)
		}, 100)
	}
}

function changeNum2DefinitionIndex(num) {
	return num - 30001;
}

function fire_characteristic0() {
	console.log("ch0 fire");
}

function invoke(functionName) {
	try {
		if (typeof(eval(functionName)) == "function") {
			console.log('动态调用方法:' + functionName)
			var s = eval(functionName + "();");
		}
	} catch (err) {}

}

function changeBrand(obj) {
	let fireId = 'xilie';
	let fire = $("#" + fireId);
	fire.val(null).trigger('change');
	let pinpai;
	for (i = 0; i < obj.length; i++) {
		let option = obj[i];
		if (option.value == obj.value) {
			pinpai = option.label;
		}
	}
	fire.empty();
	fire.append('<option value="">请选择</option>')
	// queryXilie(pinpai);

	//继续激活描述变化
	desc(obj);
}

function getObjValue(obj) {
	if ($(obj).is('select')) {
		return getSelectLabelByDomcumentObject(obj);
	} else {
		return obj.value;
	}
}

function changeMaterials(num, name, values) {
	if (!num) {
		return;
	}
	let cat = changeNum2DefinitionIndex(num);
	desc_data['select'] = cat;
	label = name;
	console.log('包材编号:' + cat + '  包材名:' + label);
	for (let field of fieldsThatNeedShow) {
		let value = undefined;
		if (values != undefined) {
			value = values[field];
		}
		dealFieldUI(field, undefined, undefined, value);
	}
}

function selectChangeMaterials(obj) {
	let num = obj.value;
	let name;
	for (let i = 0; i < obj.length; i++) {
		let option = obj[i];
		if (option.value == num) {
			name = option.label;
		}
	}
	changeMaterials(num, name);
}
